/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package RunableClass.useClass;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.net.Authenticator;
import java.net.HttpURLConnection;
import java.net.URL;
import java.sql.SQLException;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.ipaddress.authenticator;

/**
 *
 * @author Anatomy
 */
public class GroupClass {
         static  StringBuffer     content        =   new StringBuffer("");
    private boolean closestate=false;
    public String CheckGroup(String Symbol) throws InterruptedException{
        GroupURL GU=new GroupURL();
        String[] Array=GU.GroupURL();
        String GandCa="";
        for(int i=0;i<GU.GroupURL().length;i++){
            

           
            
            
           /**/         try {
         System.out.println(Array[i]);
              int count2=1;
         String buffer;
        String url = Array[i],                                                         /**/                                                      
     /**/          proxy = "proxy.up.ac.th",                                                                                        /**/
     /**/          port = "8080",                                                                                                   /**/
     /**/          username = "up\\51286885",                                                                                       /**/
     /**/          password = "7489250721";                                                                                         /**/
     /**/                                                                                                                           /**/
     /**/       //URL autoIP = new URL("http://checkip.dyndns.org:8245/");                                                          /**/
     /**/       //                                                                                    /**/
     /**/                                                                                                                           /**/
     /**/     URL server = new URL(url);                                                                                            /**/
     /**/                                                                                                                           /**/
     /**/       Authenticator.setDefault(new authenticator(                                                                         /**/
     /**/                   username,password));                                                                                    /**/
     /**/       Properties systemProperties = System.getProperties();                                                               /**/
     /**/       systemProperties.setProperty("http.proxyHost",proxy);                                                               /**/
     /**/       systemProperties.setProperty("http.proxyPort",port);                                                                /**/
     /**/       HttpURLConnection connection = (HttpURLConnection)server.openConnection();                                          /**/
     /**/      connection.connect();                                                                                                /**/
     /**/                                                                                                                           /**/
     /**/                                                                                                                           /**/
     /**/       try (BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream(),"TIS-620"))) {        /**/
     /**/                                                                                                                           /**/
     /**/                                                                                                                                /**/                                                                     /**/
     /**/                content = new StringBuffer("");                                                                                                             /**/
     /**/           while((buffer = in.readLine()) != null) {                                                                     /**/  
     /**/                  count2++;                                                                                              /**/  
     /**/                                                                                                                           /**/  
     /**/                                                                                                                         /**/  
     /**/                  content.append(count2).append("-->").append(buffer).append("\n");                                      /**/
     /**/                                                                                                                         /**/      
      /**/            }
     /**/ // System.out.println(content);
                    try {
                        if(found){
                           this.readFromFile(content.toString(),Symbol);
                    
                        
                        }
                        

                        if(closestate){
                             if(i>0&&i<3||i>3&&i<7||i>7&&i<11||i>11&&i<18||i>18&&i<22||i>22&&i<25||i>25&&i<32){
                          GandCa= GU.GroupAndCa[0]+"->"+GU.GroupAndCa[i];
                           
                
                            }
                            break;
                        }
                        Thread.sleep(5000);
                  /**/
                    } catch (SQLException ex) {
                        ex.printStackTrace();
                    }
      }                                                                                                                 /**/  
     /**/             } catch (IOException io){
            System.err.println("Couldn't create help URL: " + io);
        

    } 
        }
        return GandCa;
    }
    boolean found=true;
    public static String[]sym=new String[12];String x ="";String symbolx;int Block[]=new int[12];
        public String[] readFromFile(String filename,String symbol) throws SQLException {

        
        BufferedReader bufferedReader = null;
        String xc ="";
        String ya;
       for(int i=0;i<sym.length;i++){
          // sym[i]=null;
       }
        
        try {
            //Construct the BufferedReader object
            bufferedReader = new BufferedReader(new StringReader(filename));
            String line = null;
          
            while ((line = bufferedReader.readLine()) != null) {
                  int count1 = 0;int b[] = new int[1000];int c[] =new int[1000];int count2 = 0;
                //System.out.println(line.toString());
                            String linenumber = "";
                            String y ="";
                         //   http://www.settrade.com/C13_FastQuote_Main.jsp?txtSymbol=CM
                           // //http://www.settrade.com/C13_FastQuote_Main.jsp?txtSymbol=CSL
                           // System.out.println("/C13_FastQuote_Main.jsp?txtSymbol="+symbol+"\"");
                    if(line.contains("/C13_FastQuote_Main.jsp?txtSymbol="+symbol+"\"")){
                        found=false;
                       System.out.println(symbol);
                     } 
                       
                    
                 
 
        
            
        if(found==false){
             closestate=true;
            break;
            
       }
            }
        }
 catch (FileNotFoundException ex) {
            Logger.getLogger(GroupClass.class.getName()).log(Level.SEVERE, null, ex);
        }         catch (IOException ex) {
            ex.printStackTrace();
        } finally {
            //Close the BufferedReader
            try {
                if (bufferedReader != null)
                    bufferedReader.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        return sym;
    }
        
          public static boolean isDigit(String s)   {
     boolean right = false;
      char c = 0;
      for (int index = 0; index < s.length(); index ++)  {
            c = s.charAt(index);
            if (c >= '0' || c <= '9'||c=='.')  {
            right = true;
           }
       }
      return right;
}
    public static void main(String [] args) throws InterruptedException{
      //  new GroupClass().CheckGroup();
    }
    
   // public static void SetGroup2txt(){
        
  //  }
}
